package org.jsola.hr.helper;

import com.alibaba.fastjson.JSON;
import org.jsola.hr.dto.attendschedule.AttendPeriodDTO;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 考勤时段
 */
@Component
public class AttendPeriodTestHelper {

    /**
     * 获取考勤时段
     *
     * @return
     */
    public List<AttendPeriodDTO> getAttendPeriod() {

        List<AttendPeriodDTO> list = new ArrayList<AttendPeriodDTO>();

        for (int i = 0; i < 4; i++) {

            AttendPeriodDTO p1 = new AttendPeriodDTO();
            p1.setStartPeriod(new SimpleDateFormat("HH:mm").format(randomDate("2019-01-01","2019-01-31")));
            p1.setEndPeriod(new SimpleDateFormat("HH:mm").format(randomDate("2019-01-01","2019-01-31")));

            if (i % 2 == 0) {
                p1.setType(1);
            }
            else {
                p1.setType(2);
            }
            list.add(p1);
        }

        return list;
    }

    public List<AttendPeriodDTO> getAttendPeriod2() {

        List<AttendPeriodDTO> list = new ArrayList<AttendPeriodDTO>();

        AttendPeriodDTO p1 = new AttendPeriodDTO();
        p1.setStartPeriod("09:00");
        p1.setEndPeriod("10:00");
        p1.setType(1);
        p1.setSort(1);
        list.add(p1);

        return list;
    }

    private static Date randomDate(String beginDate, String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }





}
